package com.weixin.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.BeanHandler;

import com.google.gson.Gson;
import com.weixin.service.IUsersService;
import com.weixin.service.impl.UsersServiceImpl;
import com.weixin.util.C3p0Util;
import com.weixin.vo.Kiting;
import com.weixin.vo.Users;

@WebServlet("/UsersServlet/*")
public class UsersServlet extends HttpServlet{
	IUsersService userService = new UsersServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo().substring(1);
		if("findById".equals(pathInfo)){
            findById(request,response);
        }else if("insert".equals(pathInfo)) {
        	insert(request,response);
        }else if("findByName".equals(pathInfo)) {
        	findByName(request,response);
        }
        
	}
	
	/**
	 * 根据姓名查找信息，通过ajax接收展示到前端，让它获取值
	 * @param request
	 * @param response
	 */
	private void findByName(HttpServletRequest request, HttpServletResponse response) {
		try {	
	        request.setCharacterEncoding("utf-8");
	        response.setCharacterEncoding("utf-8");
	        response.setContentType("text/html;charset=utf-8");
	        //获取值
			String name = request.getParameter("name");
	        String pwd = request.getParameter("pwd");
	        Users userList = userService.findById(name, pwd);
	            if(userList!=null){
	    			response.getWriter().println("["+new Gson().toJson(userList)+"]");
	            }else{
	            	response.getWriter().println(0);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	/**
	 * 修改后的信息然后再上传到数据库
	 * @param request
	 * @param response
	 */
	private void insert(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			/*获取*/
			String user_name = request.getParameter("name");
			String user_pwd = request.getParameter("user_pass");
			String bank_name = request.getParameter("bank_name");
			String user_cardNo = request.getParameter("comment");
			
			int money1 =  0;
			if(request.getParameter("user_money")!=null&&!("".equals(request.getParameter("user_money")))) {
				money1 = Integer.valueOf(request.getParameter("user_money"));
			}
			int money2= 0;
			if(request.getParameter("money")!=null&&!("".equals(request.getParameter("money")))) {
				money2 = Integer.valueOf(request.getParameter("money"));
			}
			int user_money = money1-money2;
			/*放值*/
			Kiting vo = new Kiting();
			Users vo1 = new Users();
			
			vo.setUser_name(user_name);
			vo.setBank_name(bank_name);
			vo.setUser_cardNo(user_cardNo);
			vo.setMoney(money2);
			vo.setUser_money(user_money);
			
			vo1.setUser_name(user_name);
			vo1.setUser_pwd(user_pwd);
			vo1.setUser_money(user_money);
			if(money2==0) {
				response.getWriter().println(2);
			}else if(userService.insert(vo)&&userService.update(vo1)) {
					response.getWriter().println(1);
			}else {
					response.getWriter().println(0);
					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过Id查找信息后ajax接收  然后给前端的表单赋值
	 * @param request
	 * @param response
	 */
	private void findById(HttpServletRequest request, HttpServletResponse response) {
		try {	
	        request.setCharacterEncoding("utf-8");
	        response.setCharacterEncoding("utf-8");
	        response.setContentType("text/html;charset=utf-8");
			String name = request.getParameter("name");
	        String pwd = request.getParameter("pwd");
	        Users userList = userService.findById(name, pwd);
	        if(userList!=null){
	    		response.getWriter().println("["+new Gson().toJson(userList)+"]");
	        }else{
	            response.getWriter().println(0);
	        }
	        
	     } catch (Exception e) {
	            e.printStackTrace();
	        }
		
	}
	
}
